package com.sskj.aikeeper.hangqing.activity;

import android.Manifest;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.RxPermissions;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.bean.HeadBean;
import com.sskj.aikeeper.component.DaggerUserDataComponent;
import com.sskj.aikeeper.presenter.UserInfoPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.lib.widget.SelectPictureDialog;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.APP_ACTIVITY_USERINFO)
public class UserInfoActivity extends BaseActivity<UserInfoPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    private SelectPictureDialog selectPictureDialog;
    private String mImagePath;


    @Override
    public int getLayoutId() {
        return R.layout.app_activity_user_info;
    }

    @Override
    public UserInfoPresenter getPresenter() {
        return new UserInfoPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.app_userInfoActivity1));
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvAccount.setText(HideUtil.getPhoneHide(userData.getMobile()));
                ImageUtil.setCircleImage(BaseHttpConfig.BASE_URL + userData.getUpic(), ivHead);
                tvNickname.setText(userData.getNickname());
            } else {
                userData = null;
            }
        });
    }

    @Override
    public void initData() {
        ClickUtil.click(300, tvNickname, () -> {
            ARouter.getInstance().build(RConfig.APP_ACTIVITY_NICKNAME).navigation();
        });
        ClickUtil.click(300, ivHead, () -> {
            new RxPermissions(UserInfoActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
                    .subscribe(granted -> {
                        if (granted) {
                            selectPic();
                        }
                    });
        });
    }

    private void selectPic() {
        if (selectPictureDialog == null) {
            selectPictureDialog = new SelectPictureDialog(this);
        }
        selectPictureDialog.setOnSelectListener(new SelectPictureDialog.SelectListener() {
            @Override
            public void selectByAlbum() {
                PictureSelector.create(UserInfoActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }

            @Override
            public void selectByPhoto() {
                PictureSelector.create(UserInfoActivity.this)
                        .openCamera(PictureMimeType.ofImage())
                        .setOutputCameraPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath())
                        .maxSelectNum(1)
                        .compress(true)
                        .minimumCompressSize(100)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            }
        });
        selectPictureDialog.show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片、视频、音频选择结果回调
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            mImagePath = TextUtils.isEmpty(selectList.get(0).getCompressPath()) ? selectList.get(0).getPath() : selectList.get(0).getCompressPath();
            mPresenter.uploadHead(new File(mImagePath));
        }
    }

    public void uploadHeadOK(HeadBean head) {
        if (userData == null) {
            return;
        }
        userData.setUpic(head.getUpic());
        userViewModel.update(userData);
    }
}
